/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import commons.Constants;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class AllDBListService extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String sql = "select a.id as db_id, a.name as db_name, a.size as db_size, a.dbms_id as dbms_id, a.sla as db_sla, b.type as dbms_type, b.vm_id, c.name as vm_name, c.address as vm_address, c.active as vm_active from db a, dbms b, vm c where a.dbms_id = b.id and b.vm_id = c.id";
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        StringBuilder xml = new StringBuilder("");
        try {
            Class.forName(Constants.DB_DRIVER);
            Connection connection = DriverManager.getConnection(Constants.DB_ADDRESS, Constants.DB_USERNAME, Constants.DB_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            xml.append("<db_list>");
            while (resultSet.next()) {
                xml.append("<db>");
                xml.append("<db_id>" + resultSet.getInt("db_id") + "</db_id>");
                xml.append("<db_name>" + resultSet.getString("db_name") + "</db_name>");
                xml.append("<db_size>" + resultSet.getInt("db_size") + "</db_size>");
                xml.append("<db_sla>" + resultSet.getInt("db_sla") + "</db_sla>");
                                
                xml.append("<dbms_id>" + resultSet.getInt("dbms_id") + "</dbms_id>");
                xml.append("<dbms_type>" + resultSet.getInt("dbms_type") + "</dbms_type>");
                
                xml.append("<vm_id>" + resultSet.getInt("vm_id") + "</vm_id>");
                xml.append("<vm_address>" + resultSet.getString("vm_address") + "</vm_address>");
                xml.append("<vm_active>" + resultSet.getBoolean("vm_active") + "</vm_active>");
                
                xml.append("</db>");
            }
            xml.append("</db_list>");
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException ex) {
            xml = new StringBuilder("");
            xml.append("<error>");
            xml.append("<message>" + ex.getMessage() + "</message>");
            xml.append("</error>");
        } catch (SQLException ex) {
            xml = new StringBuilder("");
            xml.append("<error>");
            xml.append("<message>" + ex.getMessage() + "</message>");
            xml.append("</error>");
        }
        try {
            out.print(xml.toString());
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}